import logging
import os
from datetime import datetime

# 创建日志目录（如果不存在）
LOG_DIR = "logs"
os.makedirs(LOG_DIR, exist_ok=True)

# 日志文件名（按日期命名）
LOG_FILE = os.path.join(LOG_DIR, f"app_{datetime.now().strftime('%Y%m%d')}.log")

# 配置日志
logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s - %(levelname)s - %(message)s",
    handlers=[
        logging.FileHandler(LOG_FILE),
        logging.StreamHandler()
    ]
)

logger = logging.getLogger("SmartScheduler")

def log_event(event_type, message, *args):
    """记录事件日志（支持格式化）"""
    logger.info(f"{event_type}: {message}", *args)

def log_error(message, exc_info=True):
    """记录错误日志（包含堆栈跟踪）"""
    logger.error(f"ERROR: {message}", exc_info=exc_info)

def log_startup():
    """记录应用启动日志"""
    log_event("STARTUP", "Smart Scheduler v1.0 started at %s", datetime.now().strftime("%Y-%m-%d %H:%M:%S"))

def log_shutdown():
    """记录应用关闭日志"""
    log_event("SHUTDOWN", "Application closed gracefully")